﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NorthWind.Entity;
using NorthWind.LocalLogic;

namespace NorthWind.Win
{
    public partial class frmCliente : Form
    {
        //evento creado en el formulario
        public event EventHandler<TbClienteBE> OnClienteSeleccionado;

        public frmCliente()
        {
            InitializeComponent();
        }

        List<TbClienteBE> Lista = new List<TbClienteBE>();
        
        private void frmCliente_Load(object sender, EventArgs e)
        {
            Lista = TbClienteBL.ListaTbCliente();
            ClientebindingSource.DataSource = Lista;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.RowHeadersVisible = false;            
            
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Return) {
               
                AgregarClienteaFactura();
            
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AgregarClienteaFactura();
                                
                
        }

        private void AgregarClienteaFactura()
        {
            int i = dataGridView1.CurrentRow.Index;
            decimal codigocliente =
                Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value);

            TbClienteBE oClienteDTO = (from item in Lista.ToArray()
                                       where item.CodCliente == codigocliente
                                       select item).Single();
            //le pasanos un objetos y un argumento
            OnClienteSeleccionado(new object(), oClienteDTO);
            this.Close();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            AgregarClienteaFactura();
        }

       

      

        
    }
}
